package com.ruoyi.four.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.io.JsonStringEncoder;
import com.ruoyi.common.constant.FourConstans;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.sign.Md5Utils;
import com.ruoyi.construction.domain.ConstructionSite;
import com.ruoyi.four.FourPlatformService;
import com.ruoyi.region.domain.TSysArea;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author sunli
 * @date 2024/12/15 8:47
 * @desc
 **/
@Service
public class FourPlatformServiceImpl implements FourPlatformService {


    //        请求参数，请求参数应该是 name1=value1&name2=value2 的形式

        /**
        *   通用登录
        */
    public String login(String username, String password) {

        String pass = Md5Utils.hash(password);
        String param ="userId="+username
                +"&password="+pass
                +"&onlyValidate=true";
        String s = HttpUtils.sendGet(FourConstans.BASE_URL + FourConstans.LOGIN_URL, param, "utf-8");
        System.out.println(s);
        return s;
    }


    /**
     *   添加项目工地
     * @param constructionSite 工地项目信息
     * @param sessionId  4G平台
     */
    @Override
    public String addPoi(ConstructionSite constructionSite,String sessionId) {

        JSONObject json = this.getJSONObject(constructionSite);

        String ss = new String(new JsonStringEncoder().quoteAsString((json.toString())));
        String param =
            "name="+constructionSite.getName()
            +"&dirId="+"1"
            +"&lng="+constructionSite.getLongitude()
            +"&lat="+constructionSite.getLatitude()
            +"&radius="+100
//            +"&minZoom="+4
//            +"&exts="+ss
//            +"&addFiles="+new ArrayList<>()
            +"&addr="+constructionSite.getAddress()
            +"&sessionId="+sessionId;
        String s = HttpUtils.sendPost(FourConstans.BASE_URL + FourConstans.ADD_POI_URL, param);
        System.out.println(s);
        return s;
    }

    /**
     * 查询运输公司--->车队
     * 用于检索该时间段新增车队，可为空 yyyyMMddHHmmss
     * 用于检索该时间段新增车队，可为空 yyyyMMddHHmmss
     */
    @Override
    public List teamList(Map<String, Object> objectMap) {
        String sessionId =objectMap.get("sessionId").toString();
        String param ="sessionId="+sessionId;
        if(objectMap.containsKey("teamId")){
            String  teamId = objectMap.get("teamId").toString();
            param+="&teamId="+teamId;
        }
        if(objectMap.containsKey("startTime")){
            String  startTime = objectMap.get("startTime").toString();
            param+="&startTime="+startTime;
        }
        if(objectMap.containsKey("endTime")){
            String  startTime = objectMap.get("endTime").toString();
            param+="&endTime="+startTime;
        }

        String s = HttpUtils.sendPost(FourConstans.BASE_URL + FourConstans.TEAMLIST, param);
        System.out.println(s);
        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONArray list = jsonObject.getJSONArray("list");

        return list;
    }

    /**
     *  车辆明细列表
     */
    public List getCarList(Map<String, Object> objectMap) {
        String sessionId =objectMap.get("sessionId").toString();
        String param ="sessionId="+sessionId;

        if(objectMap.containsKey("teamId")){
            String  teamId = objectMap.get("teamId").toString();
            param+="&teamId="+teamId;
        }
        if(objectMap.containsKey("carIds")){
            String  carIds = objectMap.get("carIds").toString();
            param+="&carIds="+carIds;
        }
        if(objectMap.containsKey("detail")){
            String  detail = objectMap.get("detail").toString();
            param+="&detail="+detail;
        }

        String s = HttpUtils.sendPost(FourConstans.BASE_URL + FourConstans.CAR_LIST, param);
        System.out.println(s);
        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONArray list = jsonObject.getJSONArray("list");
        return list;
    }

    /**
     * 车辆轨迹
     */
    @Override
    public List getGpsPlate(Map<String, Object> objectMap) {
        String sessionId =objectMap.get("sessionId").toString();
        String param ="sessionId="+sessionId;

        if(objectMap.containsKey("carPlate")){
            String  carPlate = objectMap.get("carPlate").toString();
            param+="&carPlate="+carPlate;
        }
        if(objectMap.containsKey("startTime")){
            String  startTime = objectMap.get("startTime").toString();
            param+="&startTime="+startTime;
        }
        if(objectMap.containsKey("endTime")){
            String  startTime = objectMap.get("endTime").toString();
            param+="&endTime="+startTime;
        }

        String s = HttpUtils.sendPost(FourConstans.BASE_URL + FourConstans.GET_GPS_PLATE, param);
        System.out.println(s);
        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONArray list = jsonObject.getJSONArray("list");
        return list;
    }

    /**
     * 查询账号下车辆定位
     */
    @Override
    public List getGpsR(Map<String, Object> objectMap) {
        String sessionId =objectMap.get("sessionId").toString();
        String param ="sessionId="+sessionId;

        if(objectMap.containsKey("teamId")){
            String  teamId = objectMap.get("teamId").toString();
            param+="&teamId="+teamId;
        }
        if(objectMap.containsKey("carIds")){
            String  carIds = objectMap.get("carIds").toString();
            param+="&carIds="+carIds;
        }
        if(objectMap.containsKey("simple")){
            String  simple = objectMap.get("simple").toString();
            param+="&simple="+simple;
        }

        String s = HttpUtils.sendPost(FourConstans.BASE_URL + FourConstans.GET_GPS_R, param);
        System.out.println(s);
        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONArray list = jsonObject.getJSONArray("list");
        return list;
    }

    /**
     * 查询标注工地、消纳场
     */
    @Override
    public List getPois(Map<String, Object> objectMap) {
        String sessionId =objectMap.get("sessionId").toString();
        String param ="sessionId="+sessionId;

        String s = HttpUtils.sendPost(FourConstans.BASE_URL + FourConstans.GET_POIS_URL, param);
        System.out.println(s);
        JSONObject jsonObject = JSONObject.parseObject(s);

        JSONArray jsonArray = new JSONArray();
        JSONArray list = jsonObject.getJSONArray("list");
        if(list.size()>0){
            for (int i = 0; i < list.size(); i++) {
                JSONObject object = JSONObject.parseObject(list.get(i).toString());
                //类型：0-目录;1-标注
                String type = object.getString("type");
                if("0".equals(type)){
                    System.out.println("是目录");
                }else{
                    jsonArray.add(object);
                }
            }
        }
        return jsonArray;
    }


    // 对象状json
    private JSONObject getJSONObject(ConstructionSite constructionSite) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("province_code",constructionSite.getProvinceCode()==null?null:constructionSite.getProvinceCode());
        jsonObject.put("province_name",constructionSite.getProvinceName()==null?null:constructionSite.getProvinceName());
        jsonObject.put("city_code",constructionSite.getCityCode()==null?null:constructionSite.getCityCode());
        jsonObject.put("city_name",constructionSite.getCityName()==null?null:constructionSite.getCityName());
        jsonObject.put("area_code",constructionSite.getAreaCode()==null?null:constructionSite.getAreaCode());
        jsonObject.put("area_name",constructionSite.getAreaName()==null?null:constructionSite.getAreaName());
        jsonObject.put("town_name",constructionSite.getTownName()==null?null:constructionSite.getTownName());
        jsonObject.put("town_code",constructionSite.getTownCode()==null?null:constructionSite.getTownCode());
        jsonObject.put("jurisdiction",constructionSite.getAddress()==null?null:constructionSite.getAddress());
        jsonObject.put("name",constructionSite.getName()==null?null:constructionSite.getName());
        jsonObject.put("address",constructionSite.getAddress()==null?null:constructionSite.getAddress());
        jsonObject.put("start_date",constructionSite.getStartDate()==null?null :constructionSite.getStartDate());
        jsonObject.put("end_date",constructionSite.getEndDate()==null?null:constructionSite.getEndDate());
        jsonObject.put("capacity",constructionSite.getCapacity()==null?null:constructionSite.getCapacity());
        jsonObject.put("region_id",constructionSite.getRegionId()==null?null:constructionSite.getRegionId());
        jsonObject.put("leader",constructionSite.getLeader()==null?null:constructionSite.getLeader());
        jsonObject.put("number",constructionSite.getNumber()==null?0:constructionSite.getNumber());
        jsonObject.put("abbreviation",constructionSite.getAbbreviation()==null?null:constructionSite.getAbbreviation());
        jsonObject.put("leader_phone",constructionSite.getLeaderPhone()==null?null:constructionSite.getLeaderPhone());
        jsonObject.put("type",constructionSite.getType()==null?null:constructionSite.getType());
        jsonObject.put("organization",constructionSite.getOrganization()==null?null:constructionSite.getOrganization());
        jsonObject.put("transport_company",constructionSite.getTransportCompany()==null?null:constructionSite.getTransportCompany());
        jsonObject.put("cargo_type",constructionSite.getCargoType()==null?null:constructionSite.getCargoType());
        jsonObject.put("construction_unit",constructionSite.getConstructionUnit()==null?null:constructionSite.getConstructionUnit());
        jsonObject.put("construction_company",constructionSite.getConstructionCompany()==null?null:constructionSite.getConstructionCompany());
        jsonObject.put("design_unit",constructionSite.getDesignUnit()==null?null:constructionSite.getDesignUnit());
        jsonObject.put("design_unit_contact",constructionSite.getDesignUnitContact()==null?null:constructionSite.getDesignUnitContact());
        jsonObject.put("design_unit_phone",constructionSite.getDesignUnitPhone()==null?null:constructionSite.getDesignUnitPhone());
        jsonObject.put("supervision_unit",constructionSite.getSupervisionUnit()==null?null:constructionSite.getSupervisionUnit());
        jsonObject.put("supervision_unit_contact",constructionSite.getDesignUnitContact()==null?null:constructionSite.getDesignUnitContact());
        jsonObject.put("measurement_unit",constructionSite.getMeasurementUnit()==null?null:constructionSite.getMeasurementUnit());
        jsonObject.put("internal_transfer",constructionSite.getInternalTransfer()==null?null:constructionSite.getInternalTransfer());
        jsonObject.put("external_transfer",constructionSite.getExternalTransfer()==null?null:constructionSite.getExternalTransfer());
        jsonObject.put("project_category",constructionSite.getProjectCategory()==null?null:constructionSite.getProjectCategory());
        jsonObject.put("engineering_sand",constructionSite.getEngineeringSand()==null?null:constructionSite.getEngineeringSand());
        jsonObject.put("construction_waste",constructionSite.getConstructionWaste()==null?null:constructionSite.getConstructionWaste());
        jsonObject.put("mud",constructionSite.getMud()==null?null:constructionSite.getMud());
        jsonObject.put("srl",constructionSite.getSrl()==null?null:constructionSite.getSrl());
        jsonObject.put("contract_price",constructionSite.getContractPrice()==null?null:constructionSite.getContractPrice());
        jsonObject.put("construction_scale",constructionSite.getConstructionScale()==null?null:constructionSite.getConstructionScale());
        jsonObject.put("certificate_number",constructionSite.getCertificateNumber()==null?null:constructionSite.getCertificateNumber());
        jsonObject.put("star",constructionSite.getStar()==null?null:constructionSite.getStar());
        jsonObject.put("remark",constructionSite.getRemark()==null?null:constructionSite.getRemark());
        jsonObject.put("team_id",constructionSite.getTeamId()==null?null:constructionSite.getTeamId());
        jsonObject.put("enterprise_name",constructionSite.getEnterpriseName()==null?null:constructionSite.getEnterpriseName());
        jsonObject.put("share",constructionSite.getShare()==null?null:constructionSite.getShare());
        jsonObject.put("exts",constructionSite.getExts()==null?null:constructionSite.getExts());
        jsonObject.put("audited_user_ids",constructionSite.getAuditFlowStatus()==null?null:constructionSite.getAuditFlowStatus());
        jsonObject.put("audit_status",constructionSite.getAuditFlowStatus()==null?null:constructionSite.getAuditFlowStatus());
        jsonObject.put("callback_volume",constructionSite.getCallbackVolume()==null?null:constructionSite.getCallbackVolume());
        jsonObject.put("land_use_certificate",constructionSite.getLandUseCertificate()==null?null:constructionSite.getLandUseCertificate());
        jsonObject.put("construction_plan_permit",constructionSite.getConstructionPlanPermit()==null?null:constructionSite.getConstructionPlanPermit());
        jsonObject.put("construction_waste_discharge_per",constructionSite.getConstructionWasteDischargePermit()==null?null:constructionSite.getConstructionWasteDischargePermit());
        jsonObject.put("construction_contract",constructionSite.getConstructionContract()==null?null:constructionSite.getConstructionContract());
        jsonObject.put("construction_permit",constructionSite.getConstructionPermit()==null?null:constructionSite.getConstructionPermit());

//        jsonObject.put("soil_site_id",constructionSite.getSurveyInformationUserId()==null?null:constructionSite.getConstructionPermit());

        jsonObject.put("construction_site_id",constructionSite.getConstrucitonUserId()==null?null:constructionSite.getConstrucitonUserId());
//        jsonObject.put("short_name",constructionSite.getsh()==null?null:constructionSite.getConstrucitonUserId());
//        jsonObject.put("manager",constructionSite.getm()==null?null:constructionSite.getConstrucitonUserId());
        jsonObject.put("manager_phone",constructionSite.getDesignUnitPhone()==null?null:constructionSite.getDesignUnitPhone());
        jsonObject.put("supervising_department",constructionSite.getSupervisionUnit()==null?null:constructionSite.getSupervisionUnit());
//        jsonObject.put("site_number",constructionSite.g()==null?null:constructionSite.getSupervisionUnit());
//        jsonObject.put("site_area",constructionSite.gets)==null?null:constructionSite.getSupervisionUnit());
//        jsonObject.put("daily_processing_capacity",constructionSite.getda)==null?null:constructionSite.getSupervisionUnit());
        jsonObject.put("unloading_fee_price",constructionSite.getContractPrice()==null?null:constructionSite.getContractPrice());
//        jsonObject.put("attention_level",constructionSite.getl()==null?null:constructionSite.getContractPrice());
//        jsonObject.put("flushing_equipment",constructionSite.getf()==null?null:constructionSite.getContractPrice());
//        jsonObject.put("hardening_equipment",constructionSite.geth()==null?null:constructionSite.getContractPrice());
        jsonObject.put("isolation_fence",constructionSite.getIsImportFocus()==null?null:constructionSite.getIsImportFocus());
        jsonObject.put("project_type",constructionSite.getType()==null?null:constructionSite.getType());
        return jsonObject;
    }
}
